//
//  PersonalHomepageView.m
//  WeCommunity
//
//  Created by mac on 2017/8/10.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "PersonalHomepageView.h"
#import "MineContentListModel.h"
#import "MineListSpaceCell.h"
#import "MinePersonInfoCell.h"
#import "MineContentListCell.h"

#define NULL_ROW1_Height 16
#define NULL_ROW2_Height 21
#define Row1_Height 88
#define Row2_Height 44

@interface PersonalHomepageView ()<UITableViewDelegate,UITableViewDataSource,MinePersonInfoCellDelegate>

@property (nonatomic,strong) NSMutableArray * listArray;

@end
@implementation PersonalHomepageView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.tableView];
    }
    return self;
}

-(void)reloadWithData
{
    self.listArray = [NSMutableArray array];
    
    //用户数据
    [self.listArray addObject:@"0"];
    [self.listArray addObject:self.contact];
    [self.listArray addObject:@"1"]; //加一些其他数据进去;
    
    //更多数据
    MineContentListModel * model = [MineContentListModel new];
    model.mark = 1;
    model.contentName = KNSLocal(@"个人相册");
    [self.listArray addObject:model];

    [self.tableView reloadData];
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithHex:0xf8f8f8 alpha:1.0];
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

#pragma mark - tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id object = self.listArray[indexPath.row];
    
    if ([object isKindOfClass:[NSString class]]) {
        if ([(NSString *)object isEqualToString:@"0"]) {
            return NULL_ROW1_Height;
        }else if ([(NSString *)object isEqualToString:@"1"]){
            return NULL_ROW2_Height;
        }
    }else if ([object isKindOfClass:[ContactModel class]]) {
        return Row1_Height;
    }else if ([object isKindOfClass:[MineContentListModel class]]) {
        return Row2_Height;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id object = self.listArray[indexPath.row];
    
    if ([object isKindOfClass:[NSString class]]) {
        //隔行cell
        
        static NSString * cellID = @"mineListSpaceCell";
        MineListSpaceCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell =[[MineListSpaceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        //没有点击效果
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }else if ([object isKindOfClass:[ContactModel class]]) {
        static NSString * cellID = @"mineList1Cell";
        MinePersonInfoCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell =[[MinePersonInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.delegate = self;
        }
        
        [cell creatCellWithModel:object];
        [cell needAddTopLine:YES addBottomLine:YES];
        [cell needAddArrow:NO];
        return cell;
    }else if ([object isKindOfClass:[MineContentListModel class]]) {
        static NSString * cellID = @"mineList2Cell";
        MineContentListCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell =[[MineContentListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        [cell showMineInfoCellWithModel:object];
        [cell needAddTopLine:YES addBottomLine:YES];
        
        return cell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    id object = self.listArray[indexPath.row];
    
    if ([object isKindOfClass:[MineContentListModel class]]) {
        if ([self.delegate respondsToSelector:@selector(personalHomepageViewSelectRowAtIndex:)]) {
            [self.delegate personalHomepageViewSelectRowAtIndex:indexPath.row];
        }
    }
}

-(void)processPersonInfoCellWithSender:(UIView *)sender clickCell:(MinePersonInfoCell *)cell
{
    if ([self.delegate respondsToSelector:@selector(processPersonInfoCellWithSender:)]) {
        [self.delegate processPersonInfoCellWithSender:sender];
    }
}


@end
